#!/usr/bin/env python3

from start_program import *
from config import *
from multiprocessing import Pool
from get_micro_data import *
from bson import ObjectId

#配置mongodb的信息
client = pymongo.MongoClient(MONGO_URL)
db = client[MONGO_DB]

'''
程序开始
'''
def get_start(university_name):
    start_conn = start_program(university_name)
    fail_university_list = start_conn.get_start_program()

    return fail_university_list

'''
将所有失败的学校及其offset添加并返回
'''
def get_fail_univeristy_list(fail_university_list):
    fail_university_list_two = []
    for item in fail_university_list:
        if len(item) != 0:
            for fail_university_info in range(len(item)):
                fail_university_list_two.append(item[fail_university_info])
    return fail_university_list_two

'''
存入mongod
'''
def save_into_mongod(university_paper_data,university_name):
    for item in university_paper_data:
        db[university_name].save(item)

'''
处理连接失败的学校信息
'''
def deal_fail_univeristy_list(fail_university_list):
    fail_university_list_two = []
    for fail_university_info in fail_university_list:
        for fail_university_name,offset in fail_university_info.items():
            micro_data = get_micro_data(fail_university_name,str(offset))
            university_paper_data,status = micro_data.use_micro_interface()
            if len(university_paper_data) == 0 and status != 200:
                print("获取" + fail_university_name + "的第" + str(offset) + "条数据失败")
                fail_university_list_two.append({fail_university_name:offset})

            else:
                print("获取" + fail_university_name + "的第" + str(offset) + "条数据成功!!!!!!")
                save_into_mongod(university_paper_data,fail_university_name)
    return fail_university_list_two


'''
程序主函数
'''
def main():
    list = []
    pool = Pool()
    list.append(pool.map(get_start,UNIVERSITY_LIST))
    # print(list)
    fail_university_list = list[0]
    fail_university_list = get_fail_univeristy_list(fail_university_list)
    print(fail_university_list)
    while len(fail_university_list) > 0:
        fail_university_list = deal_fail_univeristy_list(fail_university_list)
    print("-------------------------------------------")
    print(fail_university_list)

if __name__ == '__main__':
    main()
